package com.zixuan.gulicollege.eduservice.controller;


import com.zixuan.gulicollege.common_utils.R;
import com.zixuan.gulicollege.eduservice.config.EduConstant;
import com.zixuan.gulicollege.eduservice.service.EduCourseService;
import com.zixuan.gulicollege.entity.eduservice.EduCourse;
import com.zixuan.gulicollege.entity.eduservice.vo.CourseInfoVO;
import com.zixuan.gulicollege.entity.eduservice.vo.CoursePublishVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;
import java.util.Objects;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author zixuan
 * @since 2021-05-08
 */
@RestController
@RequestMapping("/edu_service/course")
public class EduCourseController {

    @Autowired
    private EduCourseService courseService;

    /**
     * 添加课程基本信息
     */
    @PostMapping("/addCourse")
    public R addCourse(@RequestBody CourseInfoVO courseInfoVO){

        String id = courseService.saveCourseInfo(courseInfoVO);

        return R.ok().data("courseId", id);
    }

    /**
     * 根据课程id查询课程基本信息
     */
    @GetMapping("/getCourseInfo/{courseId}")
    public R getCourseInfo(@PathVariable String courseId){
        CourseInfoVO courseInfoVO = courseService.getCourseInfo(courseId);
        return R.ok().data("courseInfoVO", courseInfoVO);
    }

    /**
     * 修改课程基本信息
     */
    @PostMapping("/updateCourseInfo")
    public R updateCourseInfo(@RequestBody CourseInfoVO courseInfoVO){
        courseService.updateCourseInfo(courseInfoVO);
        return R.ok();
    }

    /**
     * 根据课程id查询课程确认信息
     */
    @GetMapping("/getPublishCourseInfo/{id}")
    public R getPublishCourseInfo(@PathVariable String id){
        CoursePublishVO coursePublishVO = courseService.publishCourseInfo(id);
        return R.ok().data("publishCourse", coursePublishVO);
    }

    /**
     * 提交课程信息
     */
    @PostMapping("/publishCourse/{id}")
    public R publishCourse(@PathVariable String id) {
        EduCourse eduCourse = new EduCourse();
        eduCourse.setId(id);
        eduCourse.setStatus(EduConstant.COURSE_STATUS_NORMAL);
        courseService.updateById(eduCourse);
        return R.ok();
    }

    /**
     * 课程信息分页显示
     * @return
     */
    @GetMapping("/getCourseInfo/{current}/{limit}")
    public R pageListCourse(
            @PathVariable Integer current,
            @PathVariable Integer limit
    ) {
        Map<String,Object> map = courseService.pageListCourse(current, limit);
        return R.ok().data(map);
    }

    /**
     * 按条件查询分页信息
     * @param current
     * @param limit
     * @param coursePublishVO
     * @return
     */
    @PostMapping("/pageCourseCondition/{current}/{limit}")
    public R pageCourseCondition(
            @PathVariable Integer current,
            @PathVariable Integer limit,
            @RequestBody(required = false) CoursePublishVO coursePublishVO
    ) {
        Map<String, Object> map = courseService.pageCourseCondition(current, limit, coursePublishVO);
        return R.ok().data(map);
    }

    /**
     * 根据id删除课程信息
     * @param id
     * @return
     */
    @DeleteMapping("/deleteCourse/{id}")
    public R deleteCourse(@PathVariable String id) {
        courseService.removeCourse(id);
        return R.ok();
    }

    /**
     * 修改课程发布信息
     * @param eduCourse
     * @return
     */
    @PostMapping("/updateCourseStatus")
    public R updateCourseStatus(@RequestBody EduCourse eduCourse) {
        String statusNormal = "Normal";
        String statusDraft = "Draft";
        if (Objects.equals(eduCourse.getStatus(), statusNormal)) {
           eduCourse.setStatus(statusDraft);
        } else {
            eduCourse.setStatus(statusNormal);
        }
        courseService.updateById(eduCourse);
        return R.ok();
    }
}

